<template>
	<view>
		<uni-popup ref="popup" type="center" border-radius="10px">

			<view class="gl-popup-view">
				<view class="gl-popup-title">
					<view class=""> {{title}} </view>
					<uni-icons class="gl-button-mini gl-button-error" type="closeempty" size="18" color="#fff"
						@tap="EventClose"></uni-icons>
				</view>
				<view class="gl-popup-body"
					style="display: grid;grid-template-columns: repeat(auto-fit, minmax(128px, 1fr)); max-width: 70vw; max-height: 70vh;">
					<view class="">
						<uni-card title="身份证人像面">
							<image slot='cover' style="width: 300px;height: 200px;border-radius: 10px;"
								:src="sConf.cos + (curData?.photoa || '/media/default/photoa.png')"></image>
							<template v-slot:actions>
								<view class="card-actions">
									<view class="card-actions-item" @click="childImageEvent">
										<uni-icons type="image" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text">预览</text>
									</view>
									<view class="card-actions-item" @click="childImageEvent">
										<uni-icons type="images" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text">裁剪</text>
									</view>
									<view class="card-actions-item" @click="childImageEvent">
										<uni-icons type="loop" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text">旋转</text>
									</view>
								</view>
							</template>
						</uni-card>
					</view>
					<view class="">
						<uni-card title="身份证国徽面">
							<image slot='cover' style="width: 300px;height: 200px;border-radius: 10px;"
								:src="sConf.cos + (curData?.photob || '/media/default/photob.png')"></image>
							<template v-slot:actions>
								<view class="card-actions">
									<view class="card-actions-item" @click="childImageEvent">
										<uni-icons type="image" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text">预览</text>
									</view>
									<view class="card-actions-item" @click="childImageEvent">
										<uni-icons type="images" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text">裁剪</text>
									</view>
									<view class="card-actions-item" @click="childImageEvent">
										<uni-icons type="loop" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text">旋转</text>
									</view>
								</view>
							</template>
						</uni-card>
					</view>
					<view class="">
						<uni-card title="审核用户资料">
							<view class="" style="display: flex;flex-direction: column;gap: 15px;">
								<!--
								<uni-datetime-picker type="date" :clear-icon="false" v-model="outtime"
									:disabled="curData?.status>0" @maskClick="dateChange">
								</uni-datetime-picker>	
								-->
								<uni-easyinput type="text" :value="curData?.name" disabled>
									<template #left>
										<view class="gl-item-title">姓名:</view>
									</template>
								</uni-easyinput>
								<uni-easyinput type="text" :value="curData?.idcard" disabled>
									<template #left>
										<view class="gl-item-title">身份号码:</view>
									</template>
								</uni-easyinput>
								<uni-easyinput type="text" :value="curData?.create" disabled>
									<template #left>
										<view class="gl-item-title">提交时间:</view>
									</template>
								</uni-easyinput>
								<uni-easyinput type="date" v-model="outtime" :disabled="curData?.status>0"
									placeholder="2025-12-06">
									<template #left>
										<view class="gl-item-title">有效期:</view>
									</template>
								</uni-easyinput>
								<view class="" style="display: flex;justify-content: space-around;padding-top: 10px;">
									<view v-if="curData?.status===0" class="gl-button gl-button-base"
										@click="EventSubmit(1)">
										<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
										<text style="font-size: small;">审核</text>
									</view>
									<view v-if="curData?.status===0" class="gl-button gl-button-error"
										@click="EventSubmit(2)">
										<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
										<text style="font-size: small;">驳回</text>
									</view>
									<view class="gl-button gl-button-warning" @tap="EventClose">
										<uni-icons type="undo" size="18" color="#fff"></uni-icons>
										<text style="font-size: small;">取消</text>
									</view>
								</view>
							</view>
						</uni-card>
					</view>
				</view>
				<view class="gl-popup-foot" style="display: flex;justify-content: flex-end;gap: 10px;"></view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				swiperH: 0,
				title: '实名认证',
				sConf: getApp().globalData,
				curData: null,
				outtime: null
			}
		},
		mounted() {
			console.log('页面挂载');
			let window = uni.getWindowInfo();
			// this.designLayout(window.windowHeight);

		},
		methods: {
			designLayout(wh) {
				this.$nextTick(() => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.gl-body').boundingClientRect();
					query.select('.gl-footer').boundingClientRect();
					query.exec((res) => {
						this.swiperH = wh - Math.ceil(res[0].top) - Math.ceil(res[1].height) - 10;
					});
				})
			},
			async loadDefData() {

			},
			open(e) {
				let today = new Date()
				let stmap = today.toISOString().split('T')
				this.outtime = stmap[0]
				this.refresh(e)
				this.$refs.popup.open()
			},
			verifyDate(e) {
				console.log('verifyDate', e);
				let pattern = /^\d{4}-\d{2}-\d{2}$/;
				if (pattern.test(e)) {
					console.log("Valid date format success");
					return true
				} else {
					console.log("Invalid date format err");
					return false
				}
			},
			childImageEvent() {
				console.log('childImageEvent');
				uni.showModal({
					title: '失败',
					content: '图像编辑功能暂未开通',
					showCancel: false
				})
			},
			EventClose() {
				this.$refs.popup.close()
			},
			EventSubmit(e) {
				console.log('EventSubmit', e);
				let data = {}
				if (e === 1) {
					if (!this.verifyDate(this.outtime)) {
						uni.showModal({
							title: '错误',
							content: '有效期日期格式错误',
							showCancel: false
						})
						return
					}
					data['status'] = 1
					data['outtime'] = this.outtime + ' 23:59:59'
				}
				if (e === 2) {
					data['status'] = 2
				}

				let that = this
				uni.showModal({
					title: '确认',
					content: '是或要进行个人实名认证审核',
					success(r) {
						if (r.confirm) {
							that.loadVerifyIdCode(data).then(res => {
								that.$emit('reload')
								that.EventClose()
							}).catch(err => {
								uni.showModal({
									title: '失败',
									content: '处理失败',
									showCancel: false
								})
							})
						}
					}
				})
			},
			loadVerifyIdCode(e) {
				console.log('loadVerifyIdCode', e);
				if (!this.curData.id) {
					uni.showToast({
						title: '加载记录ID失败',
						icon: 'error'
					})
					return
				}
				let host = this.sConf.host + '/admin/verifyStaff/' + this.curData.id + '/';
				let token = this.sConf.auth.token;
				return new Promise((resolve, reject) => {
					psMethod.authenticatorPatchSync(host, token, e).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				})
			},
			refresh(e) {
				console.log('refresh', e);
				let host = this.sConf.host + '/admin/verifyStaff/' + e + '/';
				let token = this.sConf.auth.token;
				psMethod.authenticatorGetSync(host, token).then((res) => {
					console.log('请求成功', res);
					this.curData = res.resmsg;
					if (res.resmsg.outtime) {
						this.outtime = res.resmsg.outtime
					}
				});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.card-actions {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		height: 45px;
		border-top: 1px #eee solid;
	}

	.card-actions-item {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.card-actions-item-text {
		font-size: 12px;
		color: #666;
		margin-left: 5px;
	}

	.gl-item-title {
		padding-left: 5px;
		color: #007aff;
		font-weight: bold;
	}
</style>